package com.bocomm.tracevisualization.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.bocomm.tracevisualization.api.entity.TranslateManagement;
import com.bocomm.tracevisualization.api.result.CodeMessage;
import com.bocomm.tracevisualization.global.BusinessException;
import com.bocomm.tracevisualization.mapper.TranslateManagementMapper;
import com.bocomm.tracevisualization.api.service.ITranslateManagementService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

/**
 * <p>
 * 终端翻译管理 服务实现类
 * </p>
 *
 * @author cxp
 * @since 2024-03-23
 */
@Service
public class TranslateManagementServiceImpl extends ServiceImpl<TranslateManagementMapper, TranslateManagement> implements ITranslateManagementService {

    @Override
    public String aliasIp(String ip){
        if(!StringUtils.hasText(ip)){
            return "";
        }
        QueryWrapper<TranslateManagement> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(TranslateManagement::getTerminalIp,ip);

        TranslateManagement translateManagement = getOne(queryWrapper);
        if(null == translateManagement){
            return null;
        }
        return translateManagement.getAlias();
    }

    @Override
    public void saveOrUpdateAndChack(TranslateManagement translateManagement) {
        String terminalIp = translateManagement.getTerminalIp();
        QueryWrapper<TranslateManagement> qw = new QueryWrapper<>();
            qw.lambda().eq(TranslateManagement::getTerminalIp,terminalIp);


        if(null == translateManagement.getId()){
            int count = count(qw);
            if(count>0){
                throw new BusinessException(CodeMessage.VALIDATE_ERROR.fillArgs("终端Ip重复"));
            }
        }
        saveOrUpdate(translateManagement);
    }

}
